// pages/shopcar/shopcar.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // checked: false,
    type: true,
    shopcarlist: [],
    shows: 0,
    result: [],
    allprice: 0,
    num: 0,
    isAll: false
  },

  changeeditor() {
    this.setData({
      type: !this.data.type
    })
  },
  goshopping() {
    wx.switchTab({
      url: '../home/home',
    })
  },
  onChange(event) {
    let arr = [...this.data.shopcarlist]
    arr[event.target.dataset.index].num = event.detail;
    wx.setStorageSync('ZX_SHOP', arr)
    this.setData({
      num: event.detail
    })
  },
  onChanges(event) {
    this.setData({
      result: event.detail,
      isAll: event.detail.length == this.data.shows,
      allprice: this.numAllChange(event.detail)
    })
  },

  typeClick() {
    if (this.data.type) {

    } else {
      let arr = [...this.data.shopcarlist]
      this.data.result.forEach(item => {
        arr = arr.filter(itm => {
          return itm.id != item
        })
      })
      wx.setStorageSync('ZX_SHOP', arr)
      this.setData({
        shopcarlist: arr
      })
    }
  },

  numAllChange(arr) {
    let numAll = 0;
    this.data.shopcarlist.forEach(item => {
      arr.forEach(itm => {
        itm == item.id && (numAll += (item.num * item.price))
      })
    })
    return numAll
  },

  onAllChange(e) {
    let arr = [];
    if (e.detail) {
      this.data.shopcarlist.forEach(item => {
        arr.push(item.id + "")
      });
    }
    this.setData({
      isAll: e.detail,
      result: arr,
      allprice: this.numAllChange(arr)
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(wx.getStorageSync('ZX_SHOP').length)
    this.setData({
      shopcarlist: wx.getStorageSync('ZX_SHOP'),
      shows: wx.getStorageSync('ZX_SHOP').length
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})